<!--宠物信息-->
<template>
  <div class="row">
    <div class="container">
      <el-form :inline="true" v-if="editAndDeleteButton" class="demo-form-inline"
               :style="{marginTop: '20px',float: 'left'}">
        <el-form-item>
          <el-input v-model="requestData.requestData.name" placeholder="请输入类别名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
          <el-button type="primary" @click="reset">重置</el-button>
          <el-button type="primary" @click="addVisible">新增</el-button>
        </el-form-item>
      </el-form>
      <el-table
        :data="list"
        stripe
        border
        size="mini"
        style="width: 100%;margin-top: 10px">
        <el-table-column
          type="index"
          label="序号"
          align="center"
          width="100">
        </el-table-column>

        <el-table-column
          prop="name"
          label="类别名称"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="create_user"
          align="center"
          label="创建人">
        </el-table-column>
        <el-table-column
          prop="create_date"
          align="center"
          label="创建时间">
        </el-table-column>
        <el-table-column
          v-if="editAndDeleteButton"
          fixed="right"
          label="操作"
          align="center"
          width="200">
          <template slot-scope="scope">
            <el-button size="mini" @click="editParam(scope.row)" type="warning">编辑</el-button>
            <el-button size="mini" type="danger" @click="deleteParam(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>


      <!--信息修改-->
      <el-dialog style="text-align: left" :title="addOrUpdate == 1 ? `宠物类型信息新增` : `宠物类型信息修改`" top="5vh"
                 :visible.sync="outerVisible">

        <el-form ref="form" :model="addParam" label-width="120px" style="margin-top: 30px">

          <el-form-item label="宠物名称">
            <el-input v-model="addParam.name"></el-input>
          </el-form-item>

        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="editSubmit">提交</el-button>
          <el-button type="info" @click="outerVisible = false">取消</el-button>
        </div>
      </el-dialog>


    </div>
  </div>
</template>

<script>
  export default {
    name: "infoList",
    data() {
      return {
        fileList: {
          pet_code: [],
        },
        list: [],
        total: 0,
        optionsUserId: [],
        outerVisible: false,
        approvalVisible: false,
        editAndDeleteButton: false,
        addOrUpdate: 1,//1增2改
        leaveDts: [],
        initSelect: {
        },
        requestData: {
          current: 1,
          pageSize: 10,
          requestData: {
            name: '',
          }
        },
        addParam: {
          name: '',

        },
        labelName: {
          name: '宠物类型为空,请输入！',
        },
      }
    },
    created() {
      this.initList()
      this.initButton()
    },
    mounted() {

    },
    methods: {

      checkParam() {
        for (let item in this.addParam) {
          if (this.addParam[item].length < 1) {
            return this.labelName[item];
          }
        }
        return null;
      },

      addVisible(status) {
        for (let item in this.addParam) {
          this.addParam[item] = ''
        }
        this.addOrUpdate = 1
        this.outerVisible = true
      },

      add() {
        if (this.checkParam() != null) {
          this.$message.error(this.checkParam());
          return false;
        }

        this.service.post('/tbPetTypeInfo/add'
          , {data: this.addParam})
          .then(result => {
            let data = result.data;
            if (data.code == 500) {
              this.$message.error("请求失败：" + data.data)
            }
            if (data.data > 0) {
              this.$message.success('增加成功')
              for (let item in this.addParam) {
                this.addParam[item] = '';
              }
              for (let item in this.fileList) {
                this.fileList[item] = [];
              }
              /**获取最新数据*/
              this.initList()
              /**关闭模态窗口*/
              this.outerVisible = false
            } else {
              this.$message.error('增加失败')
            }
          })
      },

      /**公共变量*/
      dataFormatter(row, column) {
        let arrayData = this.initSelect[column.property];
        for (let i = 0; i < arrayData.length; i++) {
          if (arrayData[i].value == row[column.property]) {
            return arrayData[i].label
          }
        }
      },
      editSubmit() {
        if (this.addOrUpdate == 1) {
          this.add()
          return false
        }
        this.service.post('/tbPetTypeInfo/add'
          , {data: this.addParam})
          .then(result => {
            let data = result.data;
            if (data.code == 500) {
              this.$message.error("请求失败：" + data.data)
            }
            if (data.data > 0) {
              this.$message.success('修改成功')
              this.initList();
              this.outerVisible = false;
            } else {
              this.$message.error('修改失败')
            }
          })
      },
      initList() {
        this.service.post('/tbPetTypeInfo/list'
          , this.requestData)
          .then(result => {
            this.list = result.data.data.data;
          })
      },
      editParam(row) {
        this.addOrUpdate = 2
        this.fileList.pet_code = []
        this.addParam = JSON.parse(JSON.stringify(row));
        this.fileList.pet_code.push({
          name: this.addParam.file_name,
          url: this.$a + '/tbowner/images?fileName=' + this.addParam.picture
        })
        this.outerVisible = true;
      },
      deleteParam(row) {
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.service.post('/tbPetTypeInfo/del'
            , {data: row.id})
            .then(result => {
              let data = result.data;
              if (data.data > 0) {
                this.$message({
                  type: 'success',
                  message: '删除成功!'
                });
                this.initList()
              } else {
                this.$message({
                  type: 'info',
                  message: '已取消删除'
                });
              }
            });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      initButton() {
        let userInfo = sessionStorage.getItem("userInfo");
        if (userInfo == undefined || userInfo == null) {
          this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
            confirmButtonText: '确定',
            type: 'error'
          }).then(() => {
            this.$router.push('/home/login')
          })
          return false;
        }
        let json = JSON.parse(sessionStorage.getItem("userInfo"));
        if (json.role_id == 0) {
          this.editAndDeleteButton = true;
        }
      },
      onSubmit() {
        this.initList()
      },
      reset() {
        for (let item in this.requestData.requestData) {
          this.requestData.requestData[item] = '';
        }
      },
    }
  }
</script>

<style scoped>

</style>
